﻿import VTitle from '@/components/common/MainTitle.vue'
import RTable from './SelectRef.vue'
import CTable from './CopyTable.vue'
import CData from './CopyData.vue'
// 将指定的元数据生成脚本并下载的相关操作
import DownloadWs from '@/components/WebSocketComponents/WebSocketConsole.vue'
// 扩展参数设置时选择文档模板
import GetTemplateDialog from './GetTemplateDialog.vue'
export default {
  components: { VTitle, RTable, CTable, CData, DownloadWs, GetTemplateDialog },
  data () {
    return {
      titleName: '元数据表结构维护',
      filterText: '',
      // 表的基本信息部分
      labelTableWidth: '140px',
      isEdit: false,
      // 已经创建的表进行编辑时
      createdEdit: false,
      // 元数据表的类型
      categorys: [],
      // 当前选中的元素表--是对象结构
      checkedId: '',
      // 所有的数据类型
      totalDataType: [],
      // 所有的数据类型--按类别展示
      allDataType_category: [],
      ruleForm: {
        metaname: '',
        metadesc: '',
        category_no: '',
        main_ref: '',
        main_show_col: '',
        // 用于保存字段的数组
        tableData: [
          {
            column_name: '',
            column_desc: '',
            data_type: '',
            precision: '',
            scale: '',
            is_allow_empty: '',
            reference: '',
            is_pk: '',
            is_logic_unique: false,
            sort_num: 0,
            memo: '',
            format: '',
            ext_parameters: '', // 扩展参数
            wiki: '' // 说明文档
          }
        ],
        virColumns: [{
          column_name: '',
          column_desc: '',
          data_type: '',
          precision: '',
          scale: '',
          reference: '',
          default_value: '',
          is_show: '',
          sort_num: '',
          format: '',
          memo: '',
          ui_type: ''
        }]
      },
      rules: {
        metaname: [
          {required: true, message: '元数据表名称不能为空', trigger: 'blur'},
          {pattern: /^[a-zA-Z][_a-zA-Z0-9]*$/, message: '由英文、数字、下划线组成,并以英文开头'}
        ],
        category_no: [
          {required: true, message: '元数据表类型不能为空', trigger: 'blur'}
        ],
        column_name: [
          {required: true, message: '字段名称不能为空', trigger: 'blur'},
          {pattern: /^[a-zA-Z][_a-zA-Z0-9]*$/, message: '字段名称只能由英文、数字、下划线组成', trigger: 'blur'}
        ],
        column_desc: [
          {max: 100, message: '字段描述长度不能超过100', trigger: 'blur'}
        ],
        data_type: [
          {required: true, message: '字段类型不能为空', trigger: 'blur'}
        ],
        precision: [
          {pattern: /^[1-9]\d*$/, message: '字段精度为数据类型', trigger: 'blur'}
        ],
        scale: [
          {pattern: /^[1-9]\d*$/, message: '比例标准为数据类型', trigger: 'blur'}
        ],
        sort_num: [
          {pattern: /^[0-9]\d*$/, message: '排序编号为数据类型', trigger: 'blur'}
        ],
        ext_parameters: [
          {required: true, message: '扩展参数不能为空', trigger: 'blur'}
        ],
        ext_parameters_no: [
          {required: false, message: '扩展参数不能为空', trigger: 'blur'}
        ]
      },
      // 所有的元数据表---左边的树状结构展示
      tableList: [],
      // 所有的元数据表---用于选择关联的元数据表
      allMetaTable: [],
      // 选择关联的元数据表后,可用于选择的显示的栏位
      showColumns: [],
      // 左边树形结构的配置选项
      defaultProps: {
        // 指定子树为节点对象的某个属性值
        children: 'children',
        // 指定节点标签为节点对象的某个属性值
        label: 'metadesc'
      },
      operationType: '',
      // 默认展开的节点的 key 的数组
      defaultExpanded: ['C000'],
      // 默认勾选的节点的 key 的数组
      defaultChecked: [],
      // 当前选中的节点--元数据表的uid
      currentObjectId: '',
      loading: true,
      // 选择关联信息的弹窗
      showDialog: false,
      refInfo: '',
      clickRow: [],
      // 复制表
      copyTableDialog: false,
      // 复制数据
      copyDataDialog: false,
      // 需要调用ws的弹窗相关的信息（左侧的导入和导出的功能）
      wsDialogInfo: {
        visible: false,
        title: '将指定的元数据生成脚本并下载',
        wsApiRouter: '/metadata/genscripts'
      },
      // 选择扩展参数的弹窗
      extParameters: {
        showDialog: false,
        templateId: '',
        clickRowIndex: 0
      },
      // 固定栏位
      fixColumns: [],
      fixColumnsTable: [
        { indexName: 'column_desc', name: '字段描述' },
        { indexName: 'column_name', name: '字段名称' },
        { indexName: 'data_type', name: '数据类型' },
        { indexName: 'db_type', name: '字段类型' },
        { indexName: 'format', name: '格式化' },
        { indexName: 'is_allow_empty', name: '可否为空' },
        { indexName: 'is_pk', name: '是否为pk' },
        { indexName: 'is_show', name: '是否显示' },
        { indexName: 'memo', name: '备注' },
        { indexName: 'precision', name: '精度' },
        { indexName: 'reference', name: '关联信息' },
        { indexName: 'scale', name: '比例标准' },
        { indexName: 'sort_num', name: '排序' },
        { indexName: 'ui_type', name: 'ui类型' }
      ],
      activeName: 'addColumns'
    }
  },
  computed: {
    // 将元数据表进行类别归类展示
    getData () {
      const parentArray = []
      if (this.tableList.length) {
        this.categorys.forEach(parentVal => {
          const childrenArray = []
          this.tableList.forEach(sonVal => {
            if (parentVal.category_no === sonVal.category_no) {
              childrenArray.push(sonVal)
            }
          })
          const objTem = {
            treeId: parentVal.treeId,
            metaname: parentVal.metaname,
            metadesc: parentVal.metaname,
            category_name: parentVal.category_name,
            category_no: parentVal.category_no,
            hadOperation: false,
            disabled: true,
            children: childrenArray
          }
          parentArray.push(objTem)
        })
        return parentArray
      } else {
        return this.categorys
      }
    }
  },
  created () {
    this.getDataType()
    this.getTableData()
    this.getCategorys()
    this.getReference()
    this.getFixedInfo()
  },
  methods: {
    // 表头的提示信息
    handelHelp (h, {column}) {
      if (column.labelClassName) {
        return (
          <el-tooltip effect="dark" placement="top">
            <div slot='content'>{column.labelClassName}</div>
            <span>{column.label}</span>
          </el-tooltip>
        )
      }
    },
    // 树节点的检索
    filterNode (value, data) {
      if (!value) return true
      return data.metadesc.indexOf(value) !== -1 || data.metaname.indexOf(value) !== -1
    },
    // 获取固定栏位信息
    getFixedInfo () {
      this.$get('/metadata/fix_columns').then(data => {
        this.fixColumns = data.data.result
      }).catch(error => {
        console.log(error)
      })
    },
    // 获取虚拟栏位信息
    getVirtualInfo (data) {
      this.$get('/metadata/' + data + '/virsual_columns').then(data => {
        this.ruleForm.virColumns = data.data.result
        // 对表格中的数据进行字段类型要求的匹配--获取字段的详细信息（可否填写精度等信息--用于字段的验证）
        if (this.ruleForm.virColumns.length && this.totalDataType.length) {
          this.ruleForm.virColumns.forEach((val, index) => {
            this.totalDataType.forEach(typeVal => {
              if (val.data_type === typeVal.data_type) {
                this.ruleForm.virColumns[index].typeArray = typeVal
              }
            })
          })
        }
      }).catch(error => {
        console.log(error)
      })
    },
    // 增加字段的方法
    addColumn () {
      this.ruleForm.tableData.push({
        column_name: '',
        column_desc: '',
        data_type: '',
        precision: '',
        scale: '',
        reference: '',
        is_allow_empty: true,
        is_pk: false,
        is_logic_unique: false,
        sort_num: 0,
        memo: '',
        format: '',
        ext_parameters: '', // 扩展参数
        wiki: '' // 说明文档
      })
    },
    addVirColumn () {
      this.ruleForm.virColumns.push({
        column_name: '',
        column_desc: '',
        data_type: '',
        precision: '',
        scale: '',
        reference: '',
        default_value: '',
        is_show: '',
        sort_num: '',
        format: '',
        memo: '',
        ui_type: ''
      })
    },
    // 当设置某一字段为pk时
    setPk (row) {
      if (row.is_pk) {
        row.is_allow_empty = false
      }
    },
    // 删除一条字段（删除的是新增的--非数据库中的删除）
    handleDelete (index) {
      this.$confirm('此操作将永久删除该字段, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'el-button--info'
      }).then(() => {
        this.ruleForm.tableData.splice(index, 1)
        this.$message({
          message: '删除成功',
          type: 'success'
        })
      }).catch(() => {})
    },
    handleVirDelete (index) {
      this.$confirm('此操作将永久删除该字段, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'el-button--info'
      }).then(() => {
        this.ruleForm.virColumns.splice(index, 1)
        this.$message({
          message: '删除成功',
          type: 'success'
        })
      }).catch(() => {})
    },
    // 获取所有的表的类型
    getCategorys () {
      this.$get('/metadata/category').then(data => {
        this.categorys = []
        if (data.status === 200) {
          if (data.data.result.length) {
            data.data.result.forEach(val => {
              const objTem = {
                treeId: val.category_no,
                metaname: val.category_name,
                metadesc: val.category_name,
                category_name: val.category_name,
                category_no: val.category_no,
                hadOperation: false,
                disabled: true
              }
              this.categorys.push(objTem)
            })
          }
        }
      }).catch(error => {
        console.log(error)
      })
    },
    // 关联的元数据表改变时
    mainRefTableChange (isInit) {
      if (!this.ruleForm.main_ref) {
        this.ruleForm.main_show_col = ''
        this.showColumns = []
        return
      }
      if (isInit !== 'init') {
        this.ruleForm.main_show_col = ''
      }
      for (let i = 0; i < this.tableList.length; i++) {
        if (this.ruleForm.main_ref === this.tableList[i].metauid) {
          this.showColumns = (this.tableList[i].columns && this.tableList[i].columns.length) ? JSON.parse(JSON.stringify(this.tableList[i].columns)) : []
          break
        }
      }
    },
    // 获取指定的元数据表，可选择的关联主表---需要提出本身
    getMainRef () {
      this.allMetaTable = []
      for (let i = 0; i < this.tableList.length; i++) {
        let hadPushed = false
        if (this.allMetaTable.length) {
          for (let t = 0; t < this.allMetaTable.length; t++) {
            // 如果有同类型的元数据表
            if (this.tableList[i].category_no === this.allMetaTable[t].category_no && this.tableList[i].metauid !== this.checkedId.metauid) {
              this.$set(this.allMetaTable[t].tables, this.allMetaTable[t].tables.length, this.tableList[i])
              hadPushed = true
              break
            }
          }
        }
        // 当没有同类型的元数据表时
        if ((!hadPushed || !this.allMetaTable.length) && this.tableList[i].metauid !== this.checkedId.metauid) {
          let obj = {
            category_name: this.tableList[i].category_name,
            category_no: this.tableList[i].category_no,
            tables: []
          }
          obj.tables.push(this.tableList[i])
          this.$set(this.allMetaTable, this.allMetaTable.length, obj)
        }
      }
    },
    // 获取所有的数据类型
    getDataType () {
      this.$get('/metadata/support_datatype').then(data => {
        this.totalDataType = data.data.result
        this.allDataType_category = []
        for (let i = 0; i < this.totalDataType.length; i++) {
          let hadPushed = false
          if (this.allDataType_category.length) {
            for (let t = 0; t < this.allDataType_category.length; t++) {
              // 如果有同类型的元数据表
              if (this.totalDataType[i].category === this.allDataType_category[t].category) {
                this.$set(this.allDataType_category[t].tables, this.allDataType_category[t].tables.length, this.totalDataType[i])
                hadPushed = true
                break
              }
            }
          }
          // 当没有同类型的元数据表时
          if ((!hadPushed || !this.allDataType_category.length)) {
            let obj = {
              category: this.totalDataType[i].category,
              tables: []
            }
            obj.tables.push(this.totalDataType[i])
            this.$set(this.allDataType_category, this.allDataType_category.length, obj)
          }
        }
      }).catch(error => {
        console.log(error)
      })
    },
    // 获取所有的表(树)
    getTableData () {
      this.$get('/metadata').then(data => {
        this.loading = false
        if (data.status === 200) {
          // 清空检索表的条件
          this.filterText = ''
          this.tableList = []
          let checkTable = ''
          if (data.data.result.length) {
            this.tableList = data.data.result
            this.tableList.forEach(val => {
              val.hadOperation = true
              val.treeId = val.metauid
              if (this.currentObjectId === val.metauid) {
                checkTable = val
              }
            })
            // 编辑的成功后还是选中该表
            if (checkTable) {
              this.showTableInfo(checkTable)
              // this.getVirtualInfo(checkTable)
            } else {
              this.showTableInfo(this.tableList[0])
            }
          } else {
            this.ruleForm.metaname = ''
            this.ruleForm.metadesc = ''
            this.ruleForm.category_no = ''
            this.ruleForm.main_ref = ''
            this.ruleForm.main_show_col = ''
            this.showColumns = []
            this.ruleForm.tableData = []
            this.ruleForm.virColumns = []
          }
        }
      }).catch(error => {
        console.log(error)
      })
    },
    // 获取表的关联信息
    getReference () {
      this.$get('/metadata/alltableschema').then(data => {
        if (data.data.result.code === 'success') {
          this.refInfo = data.data.result.data
        } else if (data.data.result.code === 'failed') {
          this.$message({
            message: data.data.result.msg,
            type: 'warning'
          })
        }
      }).catch(error => {
        console.log(error)
      })
    },
    // 点击获取关联信息时，打开弹窗
    selectRefFun (row) {
      this.showDialog = true
      this.clickRow = row
      this.getReference()
    },
    // 选择了关联信息时
    sureSelectRef (fromSelect) {
      this.showDialog = false
      this.clickRow.reference = fromSelect
    },
    // 复制表时
    showCopyTable () {
      if (this.checkedId) {
        if (!this.checkedId.is_created) {
          this.$message({
            message: '该表还未创建，请先创建',
            type: 'warning'
          })
        } else if (this.checkedId.is_locked) {
          this.$message({
            message: '该表已被锁定，需要解锁后才能进行复制操作',
            type: 'warning'
          })
        } else {
          // 打开复制表的弹窗
          this.copyTableDialog = true
        }
      }
    },
    // 复制表成功时
    sureCopyTable () {
      this.copyTableDialog = false
      this.getTableData()
    },
    // 复制数据时
    showCopyData () {
      if (this.checkedId) {
        if (!this.checkedId.is_created) {
          this.$message({
            message: '该表还未创建，请先创建',
            type: 'warning'
          })
        } else if (this.checkedId.is_locked) {
          this.$message({
            message: '该表已被锁定，需要解锁后才能进行复制操作',
            type: 'warning'
          })
        } else {
          // 打开复制数据的弹窗
          this.copyDataDialog = true
        }
      }
    },
    // 复制数据成功时
    sureCopyData () {
      this.copyDataDialog = false
      this.getTableData()
    },
    // 点击树状结构的表时
    handleNodeClick (data, node, self) {
      if (data.hadOperation) {
        // 当点击的不是已经选中的选框时
        if (this.checkedId !== data) {
          this.checkedId = data
          // 选择点击的节点
          this.$refs.myTree.setCheckedNodes([data])
          // 获取当前节点的信息
          this.showTableInfo(data)
        }
      }
    },
    // 展示表的内容
    showTableInfo (data) {
      this.isEdit = false
      this.ruleForm.metaname = data.metaname
      this.ruleForm.metadesc = data.metadesc
      this.ruleForm.category_no = data.category_no
      this.ruleForm.main_ref = data.main_ref
      this.ruleForm.main_show_col = data.main_show_col
      this.ruleForm.tableData = data.columns
      this.defaultChecked = []
      this.checkedId = data
      this.defaultChecked = [data.metauid]
      this.defaultExpanded = [data.category_no]
      // 对表格中的数据进行字段类型要求的匹配--获取字段的详细信息（可否填写精度等信息--用于字段的验证）
      if (this.ruleForm.tableData.length && this.totalDataType.length) {
        this.ruleForm.tableData.forEach((val, index) => {
          this.totalDataType.forEach(typeVal => {
            if (val.data_type === typeVal.data_type) {
              this.ruleForm.tableData[index].typeArray = typeVal
            }
          })
        })
      }
      if (data.metauid) {
        this.getVirtualInfo(data.metauid)
      }
      if (this.ruleForm.main_ref) {
        this.mainRefTableChange('init')
      }
      this.$nextTick(() => {
        this.getMainRef()
      })
    },
    // el-tree只能单选
    handleCheckChange (data, node, self) {
      // 点击未选中的复选框
      if (node) {
        this.checkedId = data
        this.$refs.myTree.setCheckedNodes([data])
        // this.getVirtualInfo(data)
      } else {
        // 点击已选中的复选框
        this.$refs.myTree.setCheckedNodes([this.checkedId])
        // this.getVirtualInfo(this.checkedId)
      }
    },
    // 编辑表时--分两组形式，已经创建了表编辑和未创建的表的编辑，二者可编辑的字段信息会有不同
    handleEditTable (data) {
      if (data.is_created) {
        this.createdEdit = true
      } else {
        this.isEdit = true
      }
      this.operationType = 'edit'
      this.currentObjectId = data.metauid
      this.ruleForm.metaname = data.metaname
      this.ruleForm.metadesc = data.metadesc
      this.ruleForm.category_no = data.category_no
      this.ruleForm.main_ref = data.main_ref
      this.ruleForm.main_show_col = data.main_show_col
      this.ruleForm.tableData = data.columns
      if (this.ruleForm.main_ref) {
        this.mainRefTableChange('init')
      }
    },
    // 删除表时
    handleDeleteTable (data) {
      if (!this.isEdit) {
        this.$confirm('此操作将永久删除该数据表, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          cancelButtonClass: 'el-button--info'
        }).then(() => {
          this.$delete('/metadata/' + data.metauid).then(data => {
            if (data.status === 204) {
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              this.getTableData()
            }
          }).catch(error => {
            if (error.response.status === 404) {
              this.$message.error('删除失败，请重试！')
            }
          })
        }).catch(() => {})
      } else {
        this.$message({
          message: '正在编辑中，无法进行删除操作！',
          type: 'warning'
        })
      }
    },
    // 锁定表时
    handleLockTable (data) {
      if (!this.isEdit) {
        this.$confirm('确定要锁定该表么?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          cancelButtonClass: 'el-button--info'
        }).then(() => {
          this.$patch('/metadata/' + data.metauid + '/lock').then(data => {
            if (data.data.result.code === 'failed') {
              this.$message({
                message: data.data.result.msg,
                type: 'warning'
              })
            } else if (data.data.result.code === 'success') {
              this.$message({
                message: '该表已被锁定！',
                type: 'success'
              })
              this.getTableData()
            }
          }).catch(error => {
            console.log(error)
          })
        }).catch(() => {})
      } else {
        this.$message({
          message: '正在编辑中，无法进行该操作！',
          type: 'warning'
        })
      }
    },
    // 解锁表时
    handleUnlockTable (data) {
      this.$confirm('确定要为该表解锁么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'el-button--info'
      }).then(() => {
        this.$patch('/metadata/' + data.metauid + '/unlock').then(data => {
          if (data.data.result.code === 'failed') {
            this.$message({
              message: data.data.result.msg,
              type: 'warning'
            })
          } else if (data.data.result.code === 'success') {
            this.$message({
              message: '该表已成功解锁！',
              type: 'success'
            })
            this.getTableData()
          }
        }).catch(error => {
          console.log(error)
        })
      }).catch(() => {})
    },
    // 新增元数据表时右侧显示的内容
    addTable () {
      this.isEdit = true
      this.operationType = 'add'
      this.ruleForm.metaname = ''
      this.ruleForm.metadesc = ''
      this.ruleForm.category_no = ''
      this.ruleForm.main_ref = ''
      this.ruleForm.main_show_col = ''
      this.showColumns = []
      this.ruleForm.tableData = []
      this.ruleForm.virColumns = []
      this.checkedId = ''
      this.$nextTick(() => {
        this.getMainRef()
      })
    },
    // 剔除表格中的typeArray的字段--该字段只是用于填写字段信息的验证使用
    deleteTypeArray () {
      let submitArray = []
      if (this.ruleForm.tableData.length) {
        this.ruleForm.tableData.forEach(val => {
          let objExam = {}
          for (let key in val) {
            if (key !== 'typeArray') {
              objExam[key] = val[key]
            }
          }
          submitArray.push(objExam)
        })
      }
      return submitArray
    },
    editTypeArray () {
      let editArray = []
      if (this.ruleForm.virColumns.length) {
        this.ruleForm.virColumns.forEach(val => {
          let objExam = {}
          for (let key in val) {
            if (key !== 'typeArray') {
              objExam[key] = val[key]
            }
          }
          editArray.push(objExam)
        })
        return editArray
      }
    },
    // 提交编辑虚拟栏位
    editVirsual (id) {
      if (!this.ruleForm.virColumns.length) {
        this.$message({
          message: '虚拟栏位字段不能为空，请点击新增字段按钮添加字段',
          type: 'warning'
        })
      } else {
        this.$post('/metadata/' + id + '/virsual_columns/edit', {
          columns: this.editTypeArray()
        }).then(data => {
          if (data.data.result.code === 'success') {
            this.$message({
              message: data.data.result.msg,
              type: 'success'
            })
            this.getTableData()
          }
        }).catch(error => {
          console.log(error)
        })
      }
    },
    // 提交新增表时
    submitAdd () {
      if (!this.ruleForm.tableData.length) {
        this.$message({
          message: '字段不能为空，请点击新增字段按钮添加字段',
          type: 'warning'
        })
      } else {
        this.$post('/metadata', {
          name: this.ruleForm.metaname,
          desc: this.ruleForm.metadesc,
          category_no: this.ruleForm.category_no,
          main_ref: this.ruleForm.main_ref,
          main_show_col: this.ruleForm.main_show_col,
          columns: this.deleteTypeArray()
        }).then(data => {
          if (data.data.result.code === 'failed') {
            this.$message({
              message: data.data.result.msg,
              type: 'error'
            })
          } else if (data.data.result.code === 'success') {
            this.$message({
              message: data.data.result.msg,
              type: 'success'
            })
            this.currentObjectId = data.data.result.metauid
            this.getTableData()
          }
        }).catch(error => {
          console.log(error)
        })
      }
    },
    // 提交编辑表时
    submitEdit () {
      if (!this.ruleForm.tableData.length) {
        this.$message({
          message: '字段不能为空，请点击新增字段按钮添加字段',
          type: 'warning'
        })
      } else {
        this.$patch('/metadata/' + this.currentObjectId, {
          name: this.ruleForm.metaname,
          desc: this.ruleForm.metadesc,
          category_no: this.ruleForm.category_no,
          main_ref: this.ruleForm.main_ref,
          main_show_col: this.ruleForm.main_show_col,
          columns: this.deleteTypeArray()
        }).then(data => {
          if (data.data.result.code === 'failed') {
            this.$message({
              message: data.data.result.msg,
              type: 'error'
            })
          } else if (data.data.result.code === 'success') {
            this.$message({
              message: data.data.result.msg,
              type: 'success'
            })
            this.isEdit = false
            this.getTableData()
          }
        }).catch(error => {
          console.log(error)
        })
      }
    },
    // 提交编辑已创建表的编辑内容
    submitCreatedEdit () {
      this.$patch('/metadata/' + this.currentObjectId + '/alter', {
        desc: this.ruleForm.metadesc,
        category_no: this.ruleForm.category_no,
        main_ref: this.ruleForm.main_ref,
        main_show_col: this.ruleForm.main_show_col,
        columns: this.deleteTypeArray()
      }).then(data => {
        if (data.data.result.code === 'failed') {
          this.$message({
            message: data.data.result.msg,
            type: 'error'
          })
        } else if (data.data.result.code === 'success') {
          this.$message({
            message: data.data.result.msg,
            type: 'success'
          })
          this.createdEdit = false
        }
      }).catch(error => {
        console.log(error)
      })
    },
    // 点击取消按钮时
    resetForm (forms) {
      this.$refs[forms].clearValidate()
      this.isEdit = false
      this.createdEdit = false
      this.getTableData()
    },
    // 点击确定提交的按钮时
    submitForm (forms, submitType) {
      if (this.activeName === 'virtualColumns') {
        this.editVirsual(this.currentObjectId)
      } else {
        this.$refs[forms].validate((valid) => {
          if (valid) {
            if (submitType === 'edit') {
              if (this.createdEdit) {
                this.submitCreatedEdit()
              } else {
                this.submitEdit()
              }
            } else if (submitType === 'add') {
              this.submitAdd()
            }
          } else {
            return false
          }
        })
      }
    },
    // created表时
    handleCreate () {
      if (this.checkedId) {
        if (this.checkedId.is_locked) {
          this.$message({
            message: '该表已被锁定，需要解锁后才能进行建表',
            type: 'warning'
          })
        } else {
          this.$confirm('此操作将以该表的信息为模型在数据库中创建表, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            cancelButtonClass: 'el-button--info'
          }).then(() => {
            this.$patch('/metadata/create', {
              id: this.checkedId.metauid
            }).then(data => {
              if (data.data.result.code === 'failed') {
                this.$message({
                  message: data.data.result.msg,
                  type: 'error'
                })
              } else if (data.data.result.code === 'success') {
                this.$message({
                  message: '该表已经成功创建！',
                  type: 'success'
                })
                this.getTableData()
              }
            }).catch(error => {
              console.log(error)
            })
          }).catch(() => {})
        }
      }
    },
    // drop表时
    handleDrop () {
      if (this.checkedId) {
        if (!this.checkedId.is_created) {
          this.$message({
            message: '该表还未创建，请先创建',
            type: 'warning'
          })
        } else if (this.checkedId.is_locked) {
          this.$message({
            message: '该表已被锁定，需要解锁后才能进行删除',
            type: 'warning'
          })
        } else {
          this.$confirm('此操作将删除该表的信息为模型在数据库中创建的表, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            cancelButtonClass: 'el-button--info'
          }).then(() => {
            this.$patch('/metadata/{id}/drop', {
              id: this.checkedId.metauid
            }).then(data => {
              if (data.data.result.code === 'failed') {
                this.$message({
                  message: data.data.result.msg,
                  type: 'error'
                })
              } else if (data.data.result.code === 'success') {
                this.$message({
                  message: '该表已经成功删除！',
                  type: 'success'
                })
                this.getTableData()
              }
            }).catch(error => {
              console.log(error)
            })
          }).catch(() => {})
        }
      }
    },
    // 当选中的字段类型变化时
    dataTypeChange (row, forms) {
      row.precision = ''
      row.scale = ''
      row.ext_parameters = ''
      this.$refs[forms].clearValidate()
      this.totalDataType.forEach(typeVal => {
        if (row.data_type === typeVal.data_type) {
          row.typeArray = typeVal
        }
      })
    },
    // 选择扩展参数时
    selectTemplateDoc (row, index) {
      this.extParameters.showDialog = true
      this.extParameters.templateId = row.ext_parameters
      this.extParameters.clickRowIndex = index
    },
    // 设置成功时
    getTemplateIdSuccess (fromTemplateDialog) {
      this.extParameters.showDialog = false
      this.$set(this.ruleForm.tableData[this.extParameters.clickRowIndex], 'ext_parameters', fromTemplateDialog)
    },
    // ----------------左侧导入导出相关的操作---开始
    // 导入脚本并执行-导入时
    importMetaTable_ws () {
      this.$set(this.wsDialogInfo, 'visible', true)
      this.$set(this.wsDialogInfo, 'title', '导入脚本并执行')
      this.$set(this.wsDialogInfo, 'wsApiRouter', '/metadata/importscripts')
    },
    // 将指定的元数据生成脚本并下载-导出时
    downloadMetaTable_ws () {
      this.$set(this.wsDialogInfo, 'visible', true)
      this.$set(this.wsDialogInfo, 'title', '将指定的元数据生成脚本并下载')
      this.$set(this.wsDialogInfo, 'wsApiRouter', '/metadata/genscripts')
    }
  },
  watch: {
    // 树节点的过滤
    filterText (val) {
      this.$refs.myTree.filter(val)
    }
  }
}
